package dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import dao.GoodsDAO;
import po.Goods;
import po.Order;

@Repository("gd")
public class GoodsDAOImpl implements GoodsDAO {
	@PersistenceContext(name="un")
	private EntityManager em;
	
	@Override
	public int addGoods(Goods goods) {
		// TODO Auto-generated method stub
		System.out.println("add goods");
		em.persist(goods);
		return 0;
	}



	@Override
	public List<Goods> listGoodsByBid(String bid) {
		// TODO Auto-generated method stub
		String jpql="select s from Goods s where s.bid=:bid";
		List<Goods> ls=em.createQuery(jpql)
				.setParameter("bid", bid)
				.getResultList();
		if(ls.isEmpty()) return null;
		else return ls;
	}



	@Override
	public int delGoods(String gid) {
		// TODO Auto-generated method stub
		System.out.println("pulloff goods");
		String jpql="select s from Goods s where s.gid=:gid";
		List<Goods> ls=em.createQuery(jpql)
				.setParameter("gid", gid)
				.getResultList();
		if(ls.isEmpty())
			return 0;
		
		for(int i=0;i<ls.size();i++){
			em.remove(ls.get(i));
		}
		em.flush();
		return 0;
	}

}
